<?xml version="1.0" encoding="iso-8859-1"?>

<!DOCTYPE html 
     PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
  <head>
    <title>Class: ActiveBackground::Chef</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link rel="stylesheet" href="../.././rdoc-style.css" type="text/css" media="screen" />

    <script language="JavaScript" type="text/javascript">
    // <![CDATA[

        function toggleSource( id )
        {
          var elem
          var link

          if( document.getElementById )
          {
            elem = document.getElementById( id )
            link = document.getElementById( "l_" + id )
          }
          else if ( document.all )
          {
            elem = eval( "document.all." + id )
            link = eval( "document.all.l_" + id )
          }
          else
            return false;

          if( elem.style.display == "block" )
          {
            elem.style.display = "none"
            link.innerHTML = "show source"
          }
          else
          {
            elem.style.display = "block"
            link.innerHTML = "hide source"
          }
        }

        function openCode( url )
        {
          window.open( url, "SOURCE_CODE", "width=400,height=400,scrollbars=yes" )
        }
      // ]]>
    </script>

  </head>

  <body>
  <table width="100%" border='0' cellpadding='0' cellspacing='0' class='banner'><tr>
  <td class="file-title"><span class="file-title-prefix">Class</span><br />ActiveBackground::Chef</td>
  <td align="right">
    <table cellspacing=0 cellpadding=2>
      <tr valign="top">
        <td>In:</td>
        <td>

<a href="../../files/vendor/plugins/active_background/tasks/__/lib/active_background/handlers/chef_rb.html">vendor/plugins/active_background/tasks/../lib/active_background/handlers/chef.rb</a>
        </td>
      </tr>
    <tr>
      <td>Parent:</td>
      <td>
        <a href="Handler.html">
ActiveBackground::Handler
         </a>

     </td>
   </tr>
         </table>
        </td>
        </tr>
      </table>
 <!-- banner header -->

  <div id="bodyContent">
      <div id="content">




  <div class="sectiontitle">Methods</div>
  <ul>
  <li><a href="#M000068">get_worker_class</a></li>
  <li><a href="#M000065">new</a></li>
  <li><a href="#M000066">new_worker</a></li>
  <li><a href="#M000067">remove_worker</a></li>
  </ul>








<div class="sectiontitle">Public Class methods</div>
<div class="method">
  <div class="title">
    <a name="M000065"></a><b>new</b>()
  </div>
  <div class="description">

  <p>
initialize implementation by including the default worker directories
</p>
  </div>
<div class="sourcecode">
  <p class="source-link">[ <a href="javascript:toggleSource('M000065_source')" id="l_M000065_source">show source</a> ]</p>

  <div id="M000065_source" class="dyn-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/active_background/tasks/../lib/active_background/handlers/chef.rb, line 5</span>
 5:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">initialize</span>
 6:       <span class="ruby-keyword kw">super</span>
 7:       <span class="ruby-ivar">@config</span> = <span class="ruby-constant">ActiveBackground</span><span class="ruby-operator">::</span><span class="ruby-constant">Configuration</span>.<span class="ruby-identifier">instance</span>
 8:       <span class="ruby-ivar">@middle_man</span> = <span class="ruby-constant">ActiveBackground</span><span class="ruby-operator">::</span><span class="ruby-constant">MiddleManProxy</span>.<span class="ruby-identifier">instance</span>
 9:       <span class="ruby-ivar">@logger</span> = <span class="ruby-ivar">@config</span>.<span class="ruby-identifier">logger</span>
10:       
11:       <span class="ruby-constant">Dir</span>.<span class="ruby-identifier">foreach</span>(<span class="ruby-constant">File</span>.<span class="ruby-identifier">expand_path</span>(<span class="ruby-constant">File</span>.<span class="ruby-identifier">dirname</span>(<span class="ruby-keyword kw">__FILE__</span>) <span class="ruby-operator">+</span> <span class="ruby-value str">&quot;/../../../workspace/workers/&quot;</span>)) <span class="ruby-keyword kw">do</span> <span class="ruby-operator">|</span><span class="ruby-identifier">file</span><span class="ruby-operator">|</span>
12:         <span class="ruby-keyword kw">if</span> <span class="ruby-identifier">file</span> <span class="ruby-operator">!=</span> <span class="ruby-value str">&quot;.&quot;</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">file</span> <span class="ruby-operator">!=</span> <span class="ruby-value str">&quot;..&quot;</span> <span class="ruby-operator">&amp;&amp;</span> <span class="ruby-identifier">file</span> <span class="ruby-operator">!=</span> <span class="ruby-value str">&quot;.svn&quot;</span>
13:           <span class="ruby-identifier">require</span> <span class="ruby-constant">File</span>.<span class="ruby-identifier">expand_path</span>(<span class="ruby-constant">File</span>.<span class="ruby-identifier">dirname</span>(<span class="ruby-keyword kw">__FILE__</span>) <span class="ruby-operator">+</span> <span class="ruby-value str">&quot;/../../../workspace/workers/&quot;</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">file</span>)
14:         <span class="ruby-keyword kw">end</span>
15:       <span class="ruby-keyword kw">end</span>
16:     <span class="ruby-keyword kw">end</span>
</pre>
  </div>
</div>
</div>

<div class="sectiontitle">Public Instance methods</div>
<div class="method">
  <div class="title">
    <a name="M000066"></a><b>new_worker</b>(worker, options={}, *argv)
  </div>
  <div class="description">

  <p>
retrieves the worker from one of the registered directories and passes it
on to the server.
</p>
<p>
Arguments:
</p>
<pre>
    &lt;tt&gt;worker&lt;/tt&gt;: Name of worker can be passed, unless
    options[:class], name is converted to class name.
    &lt;tt&gt;options&lt;/tt&gt;: Hash of options allow you to specify worker creation parameters.
    &lt;tt&gt;argv&lt;/tt&gt;: Hash of argv are passed on to the worker (without modification).
</pre>
<p>
Options:
</p>
<pre>
    &lt;tt&gt;:class&lt;/tt&gt;: Name of worker passed as a symbol
    &lt;tt&gt;:key&lt;/tt&gt;: Uniq key that is assigned to the worker (uniq key is
    genenerated if no :key is passed)
</pre>
  </div>
<div class="sourcecode">
  <p class="source-link">[ <a href="javascript:toggleSource('M000066_source')" id="l_M000066_source">show source</a> ]</p>

  <div id="M000066_source" class="dyn-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/active_background/tasks/../lib/active_background/handlers/chef.rb, line 32</span>
32:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">new_worker</span>(<span class="ruby-identifier">worker</span>, <span class="ruby-identifier">options</span>={}, <span class="ruby-operator">*</span><span class="ruby-identifier">argv</span>)
33: 
34:       <span class="ruby-identifier">klass</span> = <span class="ruby-identifier">options</span>[<span class="ruby-identifier">:class</span>] <span class="ruby-operator">||</span> <span class="ruby-identifier">worker</span>
35:       <span class="ruby-comment cmt"># convert class name (symbol) to constant and initialize</span>
36:       <span class="ruby-keyword kw">begin</span>
37:         <span class="ruby-identifier">worker</span> = <span class="ruby-identifier">get_worker_class</span>(<span class="ruby-identifier">klass</span>).<span class="ruby-identifier">new</span>(<span class="ruby-identifier">worker</span>, <span class="ruby-identifier">options</span>, <span class="ruby-identifier">argv</span>)
38:       <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">Exception</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span> <span class="ruby-comment cmt"># couldn't find worker</span>
39:         <span class="ruby-identifier">debug</span> = <span class="ruby-identifier">e</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">+</span> <span class="ruby-value str">&quot;\n&quot;</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">backtrace</span>.<span class="ruby-identifier">join</span>(<span class="ruby-value str">&quot;\n&quot;</span>)
40:         <span class="ruby-ivar">@logger</span>.<span class="ruby-identifier">warn</span>(
41:           <span class="ruby-value str">&quot;Chef: Worker does not exist\n&quot;</span> <span class="ruby-operator">+</span>
42:           <span class="ruby-identifier">debug</span> <span class="ruby-operator">||</span> <span class="ruby-value str">&quot;&quot;</span>
43:         )
44:         <span class="ruby-identifier">raise</span> <span class="ruby-value str">&quot;Chef: Worker does not exist, please make sure you have registered &quot;</span> <span class="ruby-operator">+</span>
45:               <span class="ruby-value str">&quot;the directory that contains the worker&quot;</span>
46:       <span class="ruby-keyword kw">end</span>
47: 
48:       <span class="ruby-comment cmt"># try to pass a job to the server thru MiddleMan</span>
49:       <span class="ruby-keyword kw">begin</span>
50:         <span class="ruby-comment cmt"># pass the initialized worker to the server thru the MiddleMan class</span>
51:         <span class="ruby-ivar">@middle_man</span>.<span class="ruby-identifier">add_job</span>(<span class="ruby-identifier">worker</span>.<span class="ruby-identifier">key</span>, <span class="ruby-identifier">worker</span>)
52:         <span class="ruby-comment cmt"># return the worker (job) key. This allows to fetch the worker when needed.</span>
53:         <span class="ruby-keyword kw">return</span> <span class="ruby-identifier">worker</span>.<span class="ruby-identifier">key</span>
54:       <span class="ruby-keyword kw">rescue</span> <span class="ruby-constant">Exception</span> =<span class="ruby-operator">&gt;</span> <span class="ruby-identifier">e</span> <span class="ruby-comment cmt"># something went wrong while creating the worker</span>
55:         <span class="ruby-identifier">debug</span> = <span class="ruby-identifier">e</span>.<span class="ruby-identifier">to_s</span> <span class="ruby-operator">+</span> <span class="ruby-value str">&quot;\n&quot;</span> <span class="ruby-operator">+</span> <span class="ruby-identifier">e</span>.<span class="ruby-identifier">backtrace</span>.<span class="ruby-identifier">join</span>(<span class="ruby-value str">&quot;\n&quot;</span>)
56:         <span class="ruby-ivar">@logger</span>.<span class="ruby-identifier">warn</span>(
57:           <span class="ruby-value str">&quot;Chef: Cannot create worker\n&quot;</span> <span class="ruby-operator">+</span>
58:           <span class="ruby-identifier">debug</span> <span class="ruby-operator">||</span> <span class="ruby-value str">&quot;&quot;</span>
59:         )
60:         <span class="ruby-identifier">raise</span> <span class="ruby-value str">&quot;Chef: Something went wrong while creating a worker&quot;</span> <span class="ruby-operator">+</span>
61:               <span class="ruby-value str">&quot;, please check if your configuration is correct and your server is alive&quot;</span>
62:       <span class="ruby-keyword kw">end</span>
63:     <span class="ruby-keyword kw">end</span>
</pre>
  </div>
</div>
</div>
<div class="method">
  <div class="title">
    <a name="M000067"></a><b>remove_worker</b>(key)
  </div>
<div class="sourcecode">
  <p class="source-link">[ <a href="javascript:toggleSource('M000067_source')" id="l_M000067_source">show source</a> ]</p>

  <div id="M000067_source" class="dyn-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/active_background/tasks/../lib/active_background/handlers/chef.rb, line 65</span>
65:     <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">remove_worker</span>(<span class="ruby-identifier">key</span>)
66:       <span class="ruby-ivar">@middle_man</span>.<span class="ruby-identifier">remove_job</span>(<span class="ruby-identifier">key</span>)
67:     <span class="ruby-keyword kw">end</span>
</pre>
  </div>
</div>
</div>

<div class="sectiontitle">Private Instance methods</div>
<div class="method">
  <div class="title">
    <a name="M000068"></a><b>get_worker_class</b>(klass)
  </div>
  <div class="description">

  <p>
retrieves the constant of the worker name based on a symbol that is passed
on. The following code passes back FileRetriever constant so you can
initialize it.
</p>
<pre>
  klass = get_worker_class(:file_retriever)
  @worker = klass.new
</pre>
<p>
Arguments:
</p>
<pre>
    &lt;tt&gt;klass:&lt;/tt&gt; Class name passed as a symbol
</pre>
<p>
returns constant converted from a symbol
</p>
  </div>
<div class="sourcecode">
  <p class="source-link">[ <a href="javascript:toggleSource('M000068_source')" id="l_M000068_source">show source</a> ]</p>

  <div id="M000068_source" class="dyn-source">
<pre>
    <span class="ruby-comment cmt"># File vendor/plugins/active_background/tasks/../lib/active_background/handlers/chef.rb, line 82</span>
82:       <span class="ruby-keyword kw">def</span> <span class="ruby-identifier">get_worker_class</span>(<span class="ruby-identifier">klass</span>)
83:         <span class="ruby-comment cmt"># convert the symbol to a string and split it</span>
84:         <span class="ruby-comment cmt"># to an Array and join them together by</span>
85:         <span class="ruby-comment cmt"># capitalizing the parts of the Array.</span>
86:         <span class="ruby-keyword kw">return</span> <span class="ruby-constant">Object</span>.<span class="ruby-identifier">const_get</span>(<span class="ruby-identifier">klass</span>.<span class="ruby-identifier">to_s</span>.<span class="ruby-identifier">split</span>(<span class="ruby-value str">'_'</span>).<span class="ruby-identifier">inject</span>(<span class="ruby-value str">''</span>) {<span class="ruby-operator">|</span><span class="ruby-identifier">klass_name</span>, <span class="ruby-identifier">slice</span><span class="ruby-operator">|</span> <span class="ruby-identifier">klass_name</span>  <span class="ruby-operator">&lt;&lt;</span> <span class="ruby-identifier">slice</span>.<span class="ruby-identifier">capitalize</span>})
87:       <span class="ruby-keyword kw">end</span>
</pre>
  </div>
</div>
</div>
</div>

  </div>

    </body>
</html>